from config.settings import ALLURE_HOME, ALLURE_METADATA_DIR, REPORT_OUTPUT_DIR
from abc import ABC, abstractmethod
from utils.command import Command
from logger.logger import quick_logger
import shutil


class ReportManage(ABC):
    @abstractmethod
    def version(self) -> None: ...

    @abstractmethod
    def generate_report(self) -> None: ...

    @abstractmethod
    def clean_report(self) -> None: ...


class AllureReportManage(ReportManage):
    def clean_report(self) -> None:
        shutil.rmtree(REPORT_OUTPUT_DIR)

    def version(self) -> None:
        return_code, output_stream = Command.exec_command(f'{ALLURE_HOME} --version')
        if return_code != 0:
            quick_logger.error('Allure Not Found!')
        else:
            quick_logger.info(f'{output_stream}')

    def generate_report(self) -> None:
        return_code, output_stream = Command.exec_command(
            f'{ALLURE_HOME} generate {ALLURE_METADATA_DIR} -o {REPORT_OUTPUT_DIR} --clean')
        if return_code != 0:
            quick_logger.error('Generate Report Failed!')
            quick_logger.error(output_stream)
        else:
            quick_logger.info('Generate Report Successfully!')

    @staticmethod
    def open_report() -> None:
        return_code, output_stream = Command.exec_command(f'{ALLURE_HOME} open {REPORT_OUTPUT_DIR}')
        if return_code != 0:
            quick_logger.error('Open report failed!')
            quick_logger.error(output_stream)
        else:
            quick_logger.info('Open report finished')
